package com.feeyin.radar.msg.dao.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.dao.MessageDAO;
import com.feeyin.radar.msg.model.Message;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class MessageDAOImpl extends SqlMapClientDaoSupport
  implements MessageDAO
{
  public List getPageMessagesByDynamic(Message message, Page page)
  {
    if (page == null) return null;
    message.setPageStartRow(page.getPageStartRow());
    message.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageMessagesByDynamic", message);
  }

  public Integer getPageMessagesCountByDynamic(Message message)
  {
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageMessagesCountByDynamic", message);
  }

  public Message getMessageById(Integer id) {
    if (id == null) return null;
    Message message = new Message();
    message.setId(id);
    return (Message)getSqlMapClientTemplate().queryForObject("getMessageByDynamic", message);
  }

  public Message getMessageByMessageCode(String messageCode) {
    if (StringUtils.isEmpty(messageCode)) return null;
    Message message = new Message();
    message.setMessageCode(messageCode);
    return (Message)getSqlMapClientTemplate().queryForObject("getMessageByDynamic", message);
  }

  public boolean insertMessage(Message message) {
    if (message == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertMessage", message);
    return result != null;
  }

  public boolean updateMessage(Message message)
  {
    if (message == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateMessage", message));
    return result != null;
  }

  public boolean updateMessageCommentNum(Integer id, Integer commentNum)
  {
    if (id == null) return false;
    if (commentNum == null) return false;
    Message message = new Message();
    message.setId(id);
    message.setCommentNum(commentNum);
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateMessageByDynamic", message));
    return result != null;
  }

  public boolean updateMessageViewNum(Integer id, Integer viewNum)
  {
    if (id == null) return false;
    if (viewNum == null) return false;
    Message message = new Message();
    message.setId(id);
    message.setViewNum(viewNum);
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateMessageByDynamic", message));
    return result != null;
  }

  public boolean deleteMessage(Integer id)
  {
    if (id == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteMessage", id));
    return result != null;
  }
}